package com.leilei.dao.impl;

import com.leilei.dao.ICommodityImgs;
import com.leilei.entity.CommodityImgs;
import com.leilei.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class CommodityImgsImpl implements ICommodityImgs {

    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());

    @Override
    public List<CommodityImgs> selectCommodityImgsByPid(int pid) {

        try {
            return queryRunner.query("select piid, pid, src, isindex, isbigindex from commodityimgs where pid = ?",
                    new BeanListHandler<CommodityImgs>(CommodityImgs.class), pid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public CommodityImgs selectCommodityCustom(int pid) {

        try {
            return queryRunner.query("select piid, pid, src, isindex, isbigindex from commodityimgs where pid = ? and isindex = 1",
                    new BeanHandler<CommodityImgs>(CommodityImgs.class), pid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int insertCommodityImgs(CommodityImgs commodityImgs) {

        String sql = "insert into commodityimgs(pid, src, isindex) values(?, ?, ?)";

        try {
            return queryRunner.update(sql, commodityImgs.getPid(),
                    commodityImgs.getSrc(), commodityImgs.getIsindex());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<CommodityImgs> lunboCommodityImgs(int pid) {

        try {
            return queryRunner.query("select piid, pid, src, isindex, isbigindex from commodityimgs where pid = ? and isbigindex = 1",
                    new BeanListHandler<CommodityImgs>(CommodityImgs.class), pid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

}
